package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.AuthorTypeAdapter;
import com.hengxinguotong.zhihuichengjian.adapter.MenuListAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Menu;
import com.hengxinguotong.zhihuichengjian.bean.MenuBean;
import com.hengxinguotong.zhihuichengjian.bean.AuthorType;
import com.hengxinguotong.zhihuichengjian.bean.MenuListRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 公共二级菜单
 * Created by liux on 2017/4/18.
 */

public class MenuActivity extends BaseActivity {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.menu_lv)
    ListView menuLv;
    private MenuListAdapter menuListAdapter;
    private AuthorTypeAdapter typeAdapter;
    private List<MenuBean> list;
    private List<AuthorType> mDatas;
    private String title;
    private MenuBean menu;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(menuListAdapter != null){
                menuListAdapter.setMenuBeanList(list);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        ButterKnife.bind(this);
        title = getIntent().getStringExtra("title");
        titleTv.setText(title);
        if(title.equals("证书类别") || title.equals("职位列表") || title.equals("公司类型")){
            if(title.equals("职位列表")){
                titleTv.setText("职位列表");
            }
            mDatas = (List<AuthorType>) getIntent().getSerializableExtra("list");
            typeAdapter = new AuthorTypeAdapter(mDatas, this);
            menuLv.setAdapter(typeAdapter);
            menuLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = getIntent();
                    AuthorType authorType = mDatas.get(position);
                    intent.putExtra("type", authorType);
                    setResult(1, intent);
                    finish();
                }
            });
        }else{
            list = (List<MenuBean>) getIntent().getSerializableExtra("list");
            menu = (MenuBean) getIntent().getSerializableExtra("menu");
            Iterator<MenuBean> iterator = list.iterator();
            while(iterator.hasNext()){
                MenuBean menuBean = iterator.next();
                if(menuBean.getId() == 1310 || menuBean.getId() == 1320){
                    if(menuBean.getAuth().getList() == 0){
                        iterator.remove();
                    }
                }
            }
            menuListAdapter=new MenuListAdapter(this,list);
            menuLv.setAdapter(menuListAdapter);
            menuLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    MenuBean menuBean = list.get(position);
                    ActivityJumper.jumpByURL(menuBean, MenuActivity.this);
                }
            });
        }
    }

    @OnClick({ R.id.rl_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_title:
                setResult(0);
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            setResult(0);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){
            String authority = SPUtil.getString(this, "authority");
            if(!Utils.isEmpty(authority)){
                MenuListRes menuListRes = new Gson().fromJson(authority, MenuListRes.class);
                List<MenuBean> menuList = menuListRes.getValue();
                if(menuList != null && menuList.size() > 0){
                    for (MenuBean m:menuList){
                        if(menu.getId() == m.getId()){
                            list = m.getData();
                            menuListAdapter.setMenuBeanList(list);
                        }
                    }
                }
            }
        }
    }
}
